const app = getApp()
Page({
  data: {
    loading:true,
    location:true,
    page:0,
    showbar:false,
    goodboxCurrent:1,
    cate_id: 0,
    title:app.appname,
    navgationHeight:app.globalData.navgationHeight
  },

  param:{
    cate_sid:0
  },

  //生命周期函数--监听页面加载
  onLoad: function (options){
    app.getParam(options);
    this.getIndex();
    app.getLbs((rel)=>{
      this.getApis(rel);
    })
  },
  //重新定位
  restLocation:function(e){
    if(e.currentTarget.dataset.lbs == 1){
      app.getLbs(rel => {
        this.getApis(rel);
      },true)
    }else{
      app.restLocation(rel => {
        this.getApis(rel);
      })
    }
  },
  getApis:function (rel) {
    if(200 == rel.code){
      this.setData({
        address:rel.data.address,
        location:true,
        lat:app.globalData.lbs.lat,
        lng:app.globalData.lbs.lng
      })
      this.getShop();
    }else{
      this.setData({location:false})
    }
  },
  //获取API
  getIndex: function () {
    app.api().Get('api/v1/ais/index/adwords',{ids:'index/icon'},(result) => {
      if (result.code == 200) {
        this.setData({
          adwords:result.data
        })
      }
    })
    app.api().Get('api/v1/ais/store/cate',{cate_id:0},(result) => {
      if (result.code == 200) {
        var cate = [{id:0, name:"精选"}];
        this.setData({
          cate:cate.concat(result.data)
        });
      }
    })
  },
  //获取API
  getShop: function () {
    //首页推荐
    app.api().Get('api/v1/ais/shop/index',{citycode:app.globalData.location.adcode},(result) => {
      if (result.code == 200) {
        this.setData({
          topshop: result.data,
        })
      }
    })
    //首页专题
    app.api().Get('api/v1/ais/shop/special',{citycode:app.globalData.location.adcode},(result) => {
      if (result.code == 200) {
        this.setData({
          special: result.data,
        })
      }
    })
  },
  //首页附近好店
  getStore: function () {
    if (this.data.loading) {
      var param =  {
        page: this.data.page+1,
        cate_id:this.data.cate_id,
        cate_sid:this.param.cate_sid,
        citycode:app.globalData.location.adcode
      }
      app.api().Get('api/v1/ais/store/index',param,(result) => {
        if (result.code == 200) {
          this.setData({
            stores: result.data,
            page:param.page
          })
        }
      })
      this.setData({
        loading: false
      })
    }
  },
  //产品切换到第几个了
  goodbox_swiper: function (event) {
    this.setData({
      goodboxCurrent: event.detail.current+1
    })
  },
  onSearch:function(){
    wx.navigateTo({
      url: '/packageIndex/pages/search',
    })
  },
  //点击Tab栏目切换
  onClickTab: function (event) {
    this.setData({
      cate_id: parseInt(event.detail.name),
      loading: true,
      page: 0,
      stores: [],
      sub_cate:[]
    });
    if(this.data.location && this.data.cate_id > 0){
      app.api().Get('api/v1/ais/store/cate',{cate_id:this.data.cate_id},(result) => {
        if (result.code == 200) {
          this.setData({
            sub_cate:result.data
          });
        }
      })
      this.getStore();
    }
  },
  //转发
  onShareAppMessage: function (res) {
    return {
      title: app.globalData.appConfig.shore_text,
      imageUrl: app.globalData.appConfig.shore_img,
      path: '/tab/index?ucode='+app.globalData.ucode
    }
  },
  //滚动
   onPageScroll(res) {
    let scrollTop = res.scrollTop;
    this.setData({
      showbar:38 <= scrollTop ? true : false
    })
  },
})